<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body,
        ul{
            margin: 0;
            padding: 0;
        }

        #app{
            margin: 0 auto;
            width: 750px;
        }

        ul{
            list-style: none;
        }

        span{
            width: 130px;
            display: inline-block;
        }

        h1{
            text-align: center;
        }
    </style>
</head>
<body>
    <div id="app">
        <h1>水果列表</h1>
        <ul>
            <li>
            <span>名称</span>
            <span>数量</span>
            <span>单价</span>
            <span>小计</span>
            <span>操作</span>
            </li>
            <li v-for="(item,index) in fruits":key="index">
            <span>{{item.fruitName}}</span>
            <span>{{item.fruitQuantity}}</span>
            <span>{{item.fruitPrice}}</span>
            <span>{{item.fruitQuantity*item.fruitPrice+'元'}}</span>
            <span>
                <button @click="del(index)">删除</button>
                <button @click="upData(index)">修改</button>
            </span>
            </li>
        </ul>
        <div>
            总计：{{total}}
            <p>水果名称：<input type="text" v-model="fruit.fruitName"></p><!-- 双向绑定 -->
            <p>水果数量：<input type="number" v-model="fruit.fruitQuantity"></p>
            <p>水果单价：<input type="number" v-model="fruit.fruitPrice"></p>
            <p>
                <button @click="add">{{title}}</button>
                <span >{{show}}</span>
            </p>
        </div>
    </div>
</body>
</html>
<script src="./vue.js"></script>
<script>
     var fruits = [
        {
            fruitName: '苹果',
            fruitQuantity: 30,
            fruitPrice: 3.8
        },
        {
            fruitName: '西瓜',
            fruitQuantity: 240,
            fruitPrice: 1.8
        },
        {
            fruitName: '芒果',
            fruitQuantity: 60,
            fruitPrice: 7.8
        }
    ]
</script>
<script>
    new Vue({
        el:'#app',
        data(){
            return{
                fruits,
                fruit:{
                    fruitName:'',
                    fruitQuantity:'',
                    fruitPrice:''
                },
                title:"添加水果",
                flag:-1,//-1是添加操作，非-1是修改操作
                show:""
            }
        },
        methods:{
            add:function(){
                var quantityReg=/^\d+$/
                var priceReg=/^\d+(\.\d{1,2})?$/
                if(this.fruit.fruitName==""||this.fruit.fruitQuantity==""||this.fruitPrice==""){
                    this.show="水果数据不能为空"
                    return false
                }
                if(!quantityReg.test(this.fruit.fruitQuantity)){
                    this.show="水果数量必须为整数"
                    return false
                }
                if(!priceReg.test(this.fruit.fruitPrice)){
                    this.show="水果单价必须是数值"
                    return false
                }
                if(this.flag===-1){
                    this.fruits.push(this.fruit)
                    this.fruit={}//清空添加后的数据
                }else{
                    this.fruits.splice(this.flag,1,this.fruit)
                    this.title="添加水果"
                    this.fruit={}
                    this.flag=-1
                }
                this.show=""
            },
            del:function(index){
                if (confirm("你确认要删除吗？")){//弹框
                    this.fruits.splice(index,1)
               }
            },
            upData:function(index){
                this.fruit=this.fruits[index]
                this.title="修改数据"
                this.flag=index
            }
        },
        computed:{
            total:function(){
                var sum=0;
                for(var i in this.fruits){
                    sum+=this.fruits[i].fruitQuantity*this.fruits[i].fruitPrice
                }
                return sum
            }
        }
    })
</script>